/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import net.guts.gui.application.AppLifecycleStarter;
import net.guts.gui.exception.HandlesException;
import net.guts.gui.exit.ExitController;
import at.tugraz.kmi.css.analyzer.gui.CompetenceAnalyzerMainFrame;

import com.google.inject.Inject;
import net.guts.gui.window.*;

/**
 * GUTS GUI component that starts the application
 * 
 * @author Georg Öttl
 * 
 */
class CompetenceAnalyzerLifecycleStarter implements AppLifecycleStarter {

	private final WindowController windowController;
	private final ExitController				exitController;
	private final CompetenceAnalyzerMainFrame	mainFrame;

	Logger										log;

	@Inject
	public CompetenceAnalyzerLifecycleStarter(
			WindowController windowController, ExitController exitController,
			CompetenceAnalyzerMainFrame mainFrame, Logger log) {
		this.log = log;
		this.windowController = windowController;
		this.exitController = exitController;
		this.mainFrame = mainFrame;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void startup(String[] args) {
		mainFrame.setName("mainFrame");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				exitController.shutdown();
			}
		});
		mainFrame.pack();
		windowController.show(mainFrame, JFrameConfig.create().config());
	}

	/**
	 * Handles all uncaught Throwable exceptions.
	 * @param e
	 * @return
	 */
	@HandlesException
	public boolean handle(Throwable e) {
		log.log(Level.SEVERE, e.getMessage(), e);
		return true;
	}

	/**
	 * Handles all uncaught IllegalStateException exceptions.
	 * @param e
	 * @return
	 */
	@HandlesException
	public boolean handleIllegalStateException(IllegalStateException e) {
		log.log(Level.SEVERE, e.getMessage(), e);
		return true;
	}

	/**
	 * Handles all uncaught IllegalArgumentException exceptions.
	 * @param e
	 * @return
	 */
	@HandlesException
	public boolean handle(IllegalArgumentException e) {
		log.log(Level.SEVERE, e.getMessage(), e);
		return true;
	}

	/**
	 * Handles all uncaught NullPointerException exceptions.
	 * @param e
	 * @return
	 */
	@HandlesException
	public boolean handle(NullPointerException e) {
		log.log(Level.SEVERE, e.getMessage(), e);
		return true;
	}

}
